package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {
        @Autowired
        private MongoTemplate mongoTemplate;
    @Override
    @Async
    public void saveSearchRecord(String keyword, Integer userId, String equipmentId) {
        //查询关键词是否存在
        Query query=new Query();
        //判断用户是否为空
        if (userId!=null && userId!=0){
            query.addCriteria(Criteria.where("userId").is(userId));
        }else {
            //如果为空按照手机型号查询
            query.addCriteria(Criteria.where("equipmentId").is(equipmentId));
        }
        //根据关键字查询
        query.addCriteria(Criteria.where("keyword").is(keyword));
        ApUserSearch one = mongoTemplate.findOne(query, ApUserSearch.class);
        //如果不存在,新增关键词
        if (one==null){
            ApUserSearch apUserSearch=new ApUserSearch();
            if (userId!=null && userId!=0){
                apUserSearch.setUserId(userId);
            }else {
                apUserSearch.setEquipmentId(equipmentId);
            }
            apUserSearch.setKeyword(keyword);
            apUserSearch.setCreatedTime(new Date());
            //保存
            mongoTemplate.save(apUserSearch);
        }
    }

    /**
     * 查询搜索别表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        //查询用户搜索记录,默认返回5条记录
        User user = MediaThreadLocalUtil.get();
        Integer userId=null;
        if (user!=null){
            userId=user.getUserId();
        }
        Query query=new Query();
        Criteria criteria;
        if (userId!=null){
            criteria=Criteria.where("userId").is(userId);
        }else {
            criteria=Criteria.where("equipmentId").is(dto.getEquipmentId());
        }
        query.addCriteria(criteria);
        Pageable pageable= PageRequest.of(0,5, Sort.Direction.DESC,"createdTime");
        query.with(pageable);
        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(list);
    }

    /**
     * 删除搜索列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult delete(ApArticleSearchDto dto) {
        List<ApUserSearch> hisList = dto.getHisList();
        ArrayList<String> list = new ArrayList<>();
        for (ApUserSearch userSearch : hisList) {
            list.add(userSearch.getId());
        }
        Query query=new Query();
        query.addCriteria(Criteria.where("id").in(list));
        mongoTemplate.remove(query,ApUserSearch.class);
        return ResponseResult.okResult();
    }
}
